package com.wx.springboot.step9back.controller;

import com.wx.springboot.step9back.common.api.CommonResult;
import com.wx.springboot.step9back.common.util.MyDate;
import com.wx.springboot.step9back.model.CodeTable;
import com.wx.springboot.step9back.model.Page;
import com.wx.springboot.step9back.model.User;
import com.wx.springboot.step9back.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags="用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private CodeTableService codeTableService;

    @PreAuthorize("hasAuthority('wx:user:update')")
    @ApiOperation("用户信息完善")
    @PostMapping("/perfect")
    public CommonResult perfect(@RequestBody User user){
        HashMap<String, Integer> data = new HashMap<>();
        int result = userService.perfect(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:read')")
    @ApiOperation("根据用户编号获取单个用户")
    @GetMapping("/findUserByUserCode")
    public CommonResult findUserByUserCode(@RequestParam String userCode){
        HashMap<String, Object> data = new HashMap<>();
        User user = userService.findUserByUserCode(userCode);
        data.put("data",user);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:read')")
    @ApiOperation("用户列表")
    @GetMapping("/getUserList")
    public CommonResult getUserList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.getUserList(data);
        int count = userService.getSelectCount(data);
        Page page = new Page();
        page.setList(users);
        page.setTotal(count);
        map.put("list",users);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:user:update')")
    @ApiOperation("修改用户信息")
    @PostMapping("/updateUser")
    public CommonResult updateUser(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        int result = userService.updateUser(data);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:user:delete')")
    @ApiOperation("删除用户")
    @PostMapping("/delUser")
    public CommonResult delUser(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = userService.delUser(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:update')")
    @ApiOperation("修改用户状态")
    @PostMapping("/updateState")
    public CommonResult updateState(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = userService.updateState(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:read')")
    @ApiOperation("根据小组编号获取学员")
    @GetMapping("/getUserByTeamCode")
    public CommonResult getUserByTeamCode(@RequestParam String teamCode){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = userService.getUserByTeamCode(teamCode);
        List<CodeTable> codeTables = codeTableService.getCodeByType("sign_state");
        data.put("data",users);
        data.put("code",codeTables);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:read')")
    @ApiOperation("查询未分组的学员")
    @GetMapping("/getUserItemByNoTeamCode")
    public CommonResult getUserItemByNoTeamCode(){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = userService.getUserItemByNoTeamCode();
        data.put("data",users);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:update')")
    @ApiOperation("修改用户小组")
    @PostMapping("/updateUserTeam")
    public CommonResult updateUserTeam(@RequestParam String codeTeam,@RequestParam String teamCode,@RequestParam List<Integer> ids){
        HashMap<String, Object> data = new HashMap<>();
        String result = userService.updateUserTeam(teamCode,ids);
        if (result == "success"){
            int count = userService.getCountByTeam(codeTeam);
            Map param = new HashMap();
            param.put("teamSize",count);
            param.put("teamCode",codeTeam);
            param.put("updateTime", MyDate.getDate());
            teamService.updateMemberCount(param);
            List<User> users = userService.getUserCodes(ids);
            List<String> userCodes = users.stream().map(User::getUserCode).collect(Collectors.toList());
            Map maps = new HashMap();
            maps.put("userCodes",userCodes);
            maps.put("createTime",MyDate.getDate());
            if (teamCode == "暂无"){
                maps.put("oldTeamCode",codeTeam);
                maps.put("newTeamCode",teamCode);
            } else {
                maps.put("oldTeamCode","暂无");
                maps.put("newTeamCode",codeTeam);
            }
            userService.addTransferTeams(maps);
        }
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user-student:read')")
    @ApiOperation("学员列表")
    @GetMapping("/getStudentList")
    public CommonResult getStudentList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.getStudentList(data);
        int count = userService.getStudentCount(data);
        Page page = new Page();
        page.setList(users);
        page.setTotal(count);
        map.put("list",users);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:user-student:update')")
    @ApiOperation("修改学员信息")
    @PostMapping("/updateStudent")
    public CommonResult updateStudent(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = userService.updateStudent(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user-student:delete')")
    @ApiOperation("删除学员")
    @PostMapping("/delStudent")
    public CommonResult delStudent(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int result = userService.delUser(user);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:read')")
    @ApiOperation("查询班主任")
    @GetMapping("/getTeacherList")
    public CommonResult getTeacherList(){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = userService.getTeacherList();
        data.put("data",users);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:user:read')")
    @ApiOperation("查询教练")
    @GetMapping("/getCoachList")
    public CommonResult getCoachList(){
        HashMap<String, Object> data = new HashMap<>();
        List<User> users = userService.getCoachList();
        data.put("data",users);
        return CommonResult.success(data);
    }

}
